﻿//------------------------------------------------------------------------------
// <copyright company="Microsoft Corporation">
//      Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;
using devtm.Configuration;


namespace Microsoft.Data.ConnectionUI
{
	/// <summary>
	/// Provide a default implementation for the storage of DataConnection Dialog UI configuration.
	/// </summary>
	public class DataConfiguration
	{
		private const string configFileName = @"DataConnection.xml";
		private string fullFilePath = null;
		private XDocument xDoc = null;

		// Available data sources:
		private IDictionary<string, DataSource> dataSources;

		// Available data providers: 
		private IDictionary<string, DataProvider> dataProviders;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields")]
        private DataConnectionDialog dialog;

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="path">Configuration file path.</param>
		public DataConfiguration(string path)
		{
			if (!String.IsNullOrEmpty(path))
			{
				fullFilePath = Path.GetFullPath(Path.Combine(path, configFileName));
			}
			else
			{
				fullFilePath = Path.Combine(Environment.CurrentDirectory, configFileName);
			}
			if (!String.IsNullOrEmpty(fullFilePath) && File.Exists(fullFilePath))
			{
				xDoc = XDocument.Load(fullFilePath);
			}
			else
			{
				xDoc = new XDocument();
				xDoc.Add(new XElement("ConnectionDialog", new XElement("DataSourceSelection")));
			}

			this.RootElement = xDoc.Root;
		}

		public XElement RootElement { get; set; }

        public void LoadConfiguration(DataConnectionDialog dialog, String dataSourceName, String providerName)
		{
			dialog.DataSources.Add(DataSource.SqlDataSource);
			dialog.DataSources.Add(DataSource.SqlFileDataSource);
			dialog.DataSources.Add(DataSource.OracleDataSource);
			dialog.DataSources.Add(DataSource.AccessDataSource);
			dialog.DataSources.Add(DataSource.OdbcDataSource);
			//dialog.DataSources.Add(SqlCe.SqlCeDataSource);

			dialog.UnspecifiedDataSource.Providers.Add(DataProvider.SqlDataProvider);
			dialog.UnspecifiedDataSource.Providers.Add(DataProvider.OracleDataProvider);
			dialog.UnspecifiedDataSource.Providers.Add(DataProvider.OleDBDataProvider);
			dialog.UnspecifiedDataSource.Providers.Add(DataProvider.OdbcDataProvider);
			dialog.DataSources.Add(dialog.UnspecifiedDataSource);

			this.dataSources = new Dictionary<string, DataSource>();
			this.dataSources.Add(DataSource.SqlDataSource.Name, DataSource.SqlDataSource);
			this.dataSources.Add(DataSource.SqlFileDataSource.Name, DataSource.SqlFileDataSource);
			this.dataSources.Add(DataSource.OracleDataSource.Name, DataSource.OracleDataSource);
			this.dataSources.Add(DataSource.AccessDataSource.Name, DataSource.AccessDataSource);
			this.dataSources.Add(DataSource.OdbcDataSource.Name, DataSource.OdbcDataSource);
			//this.dataSources.Add(SqlCe.SqlCeDataSource.Name, SqlCe.SqlCeDataSource);
			this.dataSources.Add(dialog.UnspecifiedDataSource.DisplayName, dialog.UnspecifiedDataSource);

			this.dataProviders = new Dictionary<string, DataProvider>();
			this.dataProviders.Add(DataProvider.SqlDataProvider.Name, DataProvider.SqlDataProvider);
			this.dataProviders.Add(DataProvider.OracleDataProvider.Name, DataProvider.OracleDataProvider);
			this.dataProviders.Add(DataProvider.OleDBDataProvider.Name, DataProvider.OleDBDataProvider);
			this.dataProviders.Add(DataProvider.OdbcDataProvider.Name, DataProvider.OdbcDataProvider);
			//this.dataProviders.Add(SqlCe.SqlCeDataProvider.Name, SqlCe.SqlCeDataProvider);

            this.dialog = dialog;
			DataSource ds = null;
            if (!String.IsNullOrEmpty(dataSourceName) && this.dataSources.TryGetValue(dataSourceName, out ds))
			{
				dialog.SelectedDataSource = ds;
			}

			DataProvider dp = null;
            if (!String.IsNullOrEmpty(providerName) && this.dataProviders.TryGetValue(providerName, out dp))
			{
				dialog.SelectedDataProvider = dp;
			}
		}


    }
}
